//
//  MAFunctionCell.m
//  meiang_edu
//
//  Created by Liubi_Chaos_G on 2018/8/1.
//  Copyright © 2018年 com.meiang.meiang_edu. All rights reserved.
//

#import "MAFunctionCell.h"
#import "MAFuncGroupModel.h"
#import "MAHomeWatchCell.h"

#define FunctionCell @"FunctionCell"
#define FunctionItemCell @"FunctionItemCell"
#define ItemW ((SCREEN_WIDTH - 48) / 4)
#define ItemH (IS_IPHONE_5 ? (ItemW + 20) : ItemW)
#define Cols 4
#define Margin 12

@interface MAFunctionCell ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *functionTitleLabel;
@property (weak, nonatomic) IBOutlet UIView *imgsContainerView;
@property (weak, nonatomic) IBOutlet UIView *bgView;



@end

@implementation MAFunctionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.bgView.layer.cornerRadius = 4;
    self.bgView.layer.masksToBounds = YES;
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] lastObject];
    }
    return self;
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    MAFunctionCell *cell = [tableView dequeueReusableCellWithIdentifier:FunctionCell];
    if (cell == nil) {
        cell = [[MAFunctionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FunctionCell];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)setGroupModel:(MAFuncGroupModel *)groupModel {
    _groupModel = groupModel;
    self.functionTitleLabel.text = groupModel.group_name;
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMG_OSS,groupModel.image]]];
    // 九宫格
    NSInteger index = groupModel.items.count;
    for (int i=0; i<index; i++) {
        MAHomeWatchCell *img = [[MAHomeWatchCell alloc] init];
        img.width = ItemW;
        img.height = ItemH;
        // 小格子x坐标
        NSInteger tempX = i % Cols;  // 小格子所在的列
        CGFloat bagX = tempX * ItemW;
        // 小格子y坐标
        NSInteger tempY = i / Cols; // 小格子所在的行
        CGFloat bagY = tempY * ItemH;
        img.x = bagX;
        img.y = bagY;
        [self.imgsContainerView addSubview:img];
        img.funcItemModel = groupModel.items[i];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgDidClick:)];
        [img addGestureRecognizer:tap];
    }
    groupModel.cellHeight = ceil(index * 1.0 / Cols) * ItemH + 40 + 5;
}

- (void)imgDidClick:(UITapGestureRecognizer *)tap {
    DLog(@"%@",tap.view);
}

@end
